package com.example.hm_dtfkz.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DQLog;
import com.android.base.util.DisplayUtil;
import com.android.base.util.ToastUtil;
import com.dq.ad.AdManager;
import com.dq.ad.UMManager;
import com.example.hm_dtfkz.R;
import com.example.hm_dtfkz.config.AppConfig;
import com.example.hm_dtfkz.config.LoginConfig;
import com.example.hm_dtfkz.databinding.DialogSaveCashBinding;
import com.example.hm_dtfkz.util.AnimUtil;

public class DialogSaveCash extends Dialog implements View.OnClickListener {

    private DialogSaveCashBinding binding;
    private OnClickLister clickLister;
    private final Activity activity;
    private double money;       //红包金额

    public DialogSaveCash(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DialogSaveCashBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView();
        initLister();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        ActivityClickUtil.getInstance().clear();
        binding.tvNum.setText((int) LoginConfig.getSaveCash() + "");
        binding.btnClose.setVisibility(View.GONE);

        if (AppConfig.saveCashState == 1) {
            binding.btnReceive.setImageResource(R.drawable.icon_dialog_save_get);
            binding.ivWord.setImageResource(R.drawable.icon_dialog_save_word2);
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.btnClose.setVisibility(View.VISIBLE);
            }
        }, 3000);

        AnimUtil.rotateAnim(binding.imgCenterBg);

        AdManager.getInstance().showInterstitialAD(activity);
    }

    private void initLister() {
        binding.btnClose.setOnClickListener(this);
        binding.btnReceive.setOnClickListener(this);

        binding.btnReceive.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Animation animation = AnimationUtils.loadAnimation(activity, R.anim.but_scale_down);
                    binding.btnReceive.startAnimation(animation);
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    Animation animation = AnimationUtils.loadAnimation(activity, R.anim.but_scale_up);
                    binding.btnReceive.startAnimation(animation);
                }
                return false;
            }
        });
    }

    public DialogSaveCash setMoney(double m) {
        this.money = m;
        return this;
    }

    public DialogSaveCash setClickLister(OnClickLister lister) {
        this.clickLister = lister;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }

        if (v.getId() == R.id.btnReceive) {
            if (AppConfig.saveCashState == 1) {
//                HXCurrencySDK.getInstance().receivePiggyBank(new HttpUtil.HttpUtilCallback() {
//                    @Override
//                    public void onSuccess(int i, String response) {
//                        DQLog.e("领取存钱罐红包券成功：" + response);
//                        if (activity.isFinishing()) {
//                            return;
//                        }
//
//                        QAPiggyBankResp qaPiggyBankResp = JsonUtil.parseObject(response, QAPiggyBankResp.class);
//                        if (qaPiggyBankResp != null) {
//                            if (qaPiggyBankResp.getErrorCode() == 0) {
//                                LoginConfig.setUserCash(qaPiggyBankResp.getRa());
//                                double deltaCash = LoginConfig.getSaveCash() - qaPiggyBankResp.getBa();
//                                LoginConfig.setSaveCash(qaPiggyBankResp.getBa());
//
//                                new DialogAddCoinResult(activity).setMoney(deltaCash).setClickLister(new DialogAddCoinResult.OnClickLister() {
//                                    @Override
//                                    public void onReceive() {
//                                        AppConfig.saveCashState = 0;
//                                        UMManager.onEvent(UMManager.Event.Receive_PiggyBankHongbao);
//                                        if (clickLister != null) {
//                                            clickLister.onReceive();
//                                        }
//                                    }
//                                }).show();
//                            } else {
//                                String errorMessage = qaPiggyBankResp.getErrorMessage();
//                                if (errorMessage.isEmpty()) {
//                                    errorMessage = "错误码=" + qaPiggyBankResp.getErrorCode();
//                                }
//                                ToastUtil.showShortToast(activity, "领取存钱罐红包券失败：" + errorMessage);
//                            }
//                        } else {
//                            ToastUtil.showShortToast(activity, "领取存钱罐红包券失败");
//                        }
//
//                        dismiss();
//                    }
//
//                    @Override
//                    public void onFailed(int i, Throwable throwable) {
//                        DQLog.e("领取存钱罐红包券失败：" + throwable.getMessage());
//                        ToastUtil.showShortToast(activity, "领取存钱罐红包券失败：" + throwable.getMessage());
//                    }
//                });
            }
        } else if (v.getId() == R.id.btnClose) {
            dismiss();
        }
    }

    public interface OnClickLister {
        void onReceive();
    }
}